package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.MysqlDBConexion;

@WebServlet("/Registrar")
public class Registrar extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public Registrar() {
        super();        
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		String nombre = req.getParameter("nombre");
		String apellido = req.getParameter("apellido");
		int edad = Integer.parseInt(req.getParameter("edad")) ;
		String carrera = req.getParameter("valorcarrera" );
		String pasatiempos = req.getParameter("valorpasatiempo");
		String sexo = req.getParameter("valorsexo" );	
		
		try {
		Connection cn=MysqlDBConexion.getConexion();
		String sql="INSERT INTO tb_alumno VALUES (null,?,?,?,?,?,?)";
		PreparedStatement pst=cn.prepareStatement(sql);
		pst.setString	(1, nombre);
		pst.setString	(2, apellido);
		pst.setInt		(3, edad);
		pst.setString	(4, carrera);
		pst.setString	(5, pasatiempos);	
		pst.setString	(6, sexo);	
		
		pst.executeUpdate(); 	
		
		RequestDispatcher rd = req.getRequestDispatcher("Logeo.jsp");
		req.setAttribute("msg", "Nuevo Alumno registrado exitosamente!");
		rd.forward(req, res);	
		} catch (Exception e) {
			e.printStackTrace();
		}		
		
	}

}
